using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The EmployeeQualtificationTableDB class is responsible for interacting with the database to retrieve and store information
/// about EmployeeQualtificationTable objects.
/// </summary>
public class EmployeeQualtificationTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EmployeeQualtificationTable from the underlying datasource.
/// </summary>
/// <param name="employeeQualtificationKey">The unique EmployeeQualtificationKey of the EmployeeQualtificationTable in the database.</param>
/// <returns>An EmployeeQualtificationTable when the EmployeeQualtificationKey was found in the database, or null otherwise.</returns>
public static EmployeeQualtificationTable GetItem(int employeeQualtificationKey)
{
EmployeeQualtificationTable myEmployeeQualtificationTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeQualtificationTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@employeeQualtificationKey", employeeQualtificationKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEmployeeQualtificationTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEmployeeQualtificationTable;
}
}

/// <summary>
/// Returns a list with EmployeeQualtificationTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeQualtificationTable objects.</returns>
public static EmployeeQualtificationTableList GetList()
{
EmployeeQualtificationTableList tempList = new EmployeeQualtificationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeQualtificationTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EmployeeQualtificationTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeQualtificationTable objects.</returns>
public static EmployeeQualtificationTableList GetListByUserAccountKey(int UserAccountKey)
{
EmployeeQualtificationTableList tempList = new EmployeeQualtificationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeQualtificationTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a EmployeeQualtificationTable in the database.
/// </summary>
/// <param name="myEmployeeQualtificationTable">The EmployeeQualtificationTable instance to save.</param>
/// <returns>The new EmployeeQualtificationKey if the EmployeeQualtificationTable is new in the database or the existing EmployeeQualtificationKey when an item was updated.</returns>
public static int Save(EmployeeQualtificationTable myEmployeeQualtificationTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeQualtificationTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEmployeeQualtificationTable.EmployeeQualtificationKey == -1)
{myCommand.Parameters.AddWithValue("@employeeQualtificationKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@employeeQualtificationKey", myEmployeeQualtificationTable.EmployeeQualtificationKey);
}
if (myEmployeeQualtificationTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myEmployeeQualtificationTable.UserAccountKey);
}
if (myEmployeeQualtificationTable.QualificationName == null){
myCommand.Parameters.AddWithValue("@qualificationName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@qualificationName", myEmployeeQualtificationTable.QualificationName);
}
if (myEmployeeQualtificationTable.Organization == null){
myCommand.Parameters.AddWithValue("@organization", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@organization", myEmployeeQualtificationTable.Organization);
}
if (myEmployeeQualtificationTable.Year == null){
myCommand.Parameters.AddWithValue("@year", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@year", myEmployeeQualtificationTable.Year);
}
if (myEmployeeQualtificationTable.GradeLevel == null){
myCommand.Parameters.AddWithValue("@gradeLevel", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@gradeLevel", myEmployeeQualtificationTable.GradeLevel);
}
if (myEmployeeQualtificationTable.Others == null){
myCommand.Parameters.AddWithValue("@others", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@others", myEmployeeQualtificationTable.Others);
}
if (myEmployeeQualtificationTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEmployeeQualtificationTable.Status);
}
if (myEmployeeQualtificationTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEmployeeQualtificationTable.CreatedDateTime);
}
if (myEmployeeQualtificationTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEmployeeQualtificationTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EmployeeQualtificationTable from the database.
/// </summary>
/// <param name="employeeQualtificationKey">The EmployeeQualtificationKey of the EmployeeQualtificationTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int employeeQualtificationKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeQualtificationTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@employeeQualtificationKey", employeeQualtificationKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the EmployeeQualtificationTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EmployeeQualtificationTable FillDataRecord(IDataRecord myDataRecord )
{
EmployeeQualtificationTable myEmployeeQualtificationTable = new EmployeeQualtificationTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmployeeQualtificationKey")))
{
myEmployeeQualtificationTable.EmployeeQualtificationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EmployeeQualtificationKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myEmployeeQualtificationTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("QualificationName")))
{
myEmployeeQualtificationTable.QualificationName = myDataRecord.GetString(myDataRecord.GetOrdinal("QualificationName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Organization")))
{
myEmployeeQualtificationTable.Organization = myDataRecord.GetString(myDataRecord.GetOrdinal("Organization"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Year")))
{
myEmployeeQualtificationTable.Year = myDataRecord.GetString(myDataRecord.GetOrdinal("Year"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("GradeLevel")))
{
myEmployeeQualtificationTable.GradeLevel = myDataRecord.GetString(myDataRecord.GetOrdinal("GradeLevel"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Others")))
{
myEmployeeQualtificationTable.Others = myDataRecord.GetString(myDataRecord.GetOrdinal("Others"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEmployeeQualtificationTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myEmployeeQualtificationTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
    myEmployeeQualtificationTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myEmployeeQualtificationTable;
}
}
